<!--#include file="lib/utf-8.asp"-->
<!--#include file="lib/connopen.asp"-->
<!--#include file="lib/asp-common-lib.asp"-->
<!--#include file="lib/upload_utf_8.asp"-->
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CC的办公系统</title>
<!--#include file="lib/plugins.asp"-->
<style type="text/css">
	.ui-datepicker table {
		border: 0;
	}
</style>
<script type="text/javascript">
	$(function() {
		$("#xls_form").validationEngine();
	});
</script>
<!--#include file="lib/js-google-analytics.asp"-->
</head>

<body>
<!--#include file="lib/header.asp"-->
<!--#include file="lib/nav.asp"-->
<div id="main">
<!--#include file="lib/col1.asp"-->
<!--#include file="lib/col2.asp"-->
	<div id="col3" role="main">
		<div id="col3_content" class="clearfix">
<%
if not CheckPostFlag(session("aaPostFlag"),1,15) then call JumpErrorPage(2)
%>
<form id="xls_form" name="xls_form" method="post" action="#" enctype="multipart/form-data">
	<input id="filepath" name="filepath" style="display:none" value="xls" />

	<table class="full">
		<tr>
			<th>上传Excel表格</th>
		</tr>
		<tr>
			<td>档案名称:<input id="xls_title" name="xls_title" />不填默认为Excel的文件名</td>
		</tr>
		<tr>
			<td>档案分类:<select id="xls_type" name="xls_type">
				<option value="2">业务档案</option>
				<option value="1">文书档案</option>
			</select></td>
		</tr>
		<tr>
			<td id="upid">选择文件:<input name="xls_file" type="file" size="60" /></td>
		</tr>
		<tr>
			<td>档案模板:<a href="xls/importfile_template.xls">下载模板</a></td>
		</tr>
		<tr>
			<td><input type="submit" name="submit" value="导入" /></td>
		</tr>
	</table>
</form>

<%
dim upload,file,formName,formPath,iCount
set upload=new upload_5xsoft
if upload.form("submit")="导入" then
	xls_title=upload.form("xls_title")
	xls_type=upload.form("xls_type")
	conn.BeginTrans
	' ON ERROR RESUME NEXT
	xls_xml=false
	up_xls=false
	formPath=upload.form("filepath")
	'xls_file=upload.form("xls_file")
	'xls_name=upload.form("xls_name")
	if right(formPath,1)<>"/" then formPath=formPath&"/" 
	'response.write(xls_file)
	'response.end
	'if right(xls_file,3)<>"xls" then
	'	response.write("非Excel文件！")
	'else
		for each formName in upload.objFile 
			set file=upload.file(formName)
			if file.FileExt<>"xls" and file.FileExt<>"xlsx" then
				response.write("非Excel文件！")
				exit for
			end if
			if file.FileExt="xlsx" then xls_xml=true
			if file.FileSize>0 then
				thistime=now()
				Randomize
				xls_name=formPath&"file_"&FormatTime(thistime,"{Y}-{m}-{d}_{h}-{n}-{s}")&"_"&int(10*rnd())&"."&file.FileExt
				up_xls=file.SaveAs(Server.mappath(xls_name))
				if up_xls then
					response.write "上传成功！"&file.FilePath&file.FileName&" ("&file.FileSize&") => <a href='"&xls_name&"'>"&xls_name&"</a><br />"
					if xls_title="" then xls_title=left(file.FileName,len(file.FileName)-4)
					xls_id=InsertXLS(xls_name,xls_title)
				end if
			end if
			set file=nothing
		next
	'end if
	'ON ERROR RESUME NEXT
	if up_xls then
		call AdoExcel(xls_name,xls_type,xls_id,xls_xml)
		
		if err.number=0 then 
			conn.CommitTrans  '如果没有conn错误，则执行事务提交
		else
			conn.RollbackTrans '否则回滚
			'回滚后的其他操作
			
			'strerr=err.Description
			url=request.ServerVariables("http_host")&request.ServerVariables("url")
			Response.Write "数据库错误！<br /><font color=red>Error # " & CStr(Err.Number) & "<br />" & Err.Description & "<br />Source: " & Err.Source &"<br />错误页面："&url&"</font><br />"
			' 删除上传的文件
			if DeleteFile(xls_name) then response.write xls_name&" =>删除成功！<br />"
			Response.End
		end if
	end if
end if
set upload=nothing 

function InsertXLS(xls_name,xls_title)
	sql="insert into xls(xls_name,xls_date,xls_type,xls_title) values('"&xls_name&"','"&date()&"',2,'"&xls_title&"')"
	InsertXLS=ExecuteCommand(sql,true)
end function


sub AdoExcel(xls_name,da_type,da_xls,xls_xml)
		response.write("开始导入"&xls_name&"...<br />")
		set xlsconn=Server.CreateObject("ADODB.Connection")
		connStr="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="&Server.MapPath(xls_name)&";Extended Properties='Excel 12.0"
		if xls_xml then connStr=connStr&" Xml"
		connStr=connStr&";HDR=YES;IMEX=1';"
		' connStr="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="&Server.MapPath(xls_name)&";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"
		xlsconn.open connStr
		set rs=server.createobject("ADODB.Recordset")
		sql="select * from [Sheet1$]"
		rs.open sql,xlsconn,1,1
		rows=0
		do while not rs.eof
			da_recordnumber=trim(rs.fields(1).value)
			da_owner=trim(rs.fields(2).value)
			da_filename=trim(rs.fields(3).value)
			da_memo=trim(rs.fields(5).value)
			if da_filename="" or isnull(da_filename) then exit do
			rows=rows+1
			' 文书档案
			if da_type=1 then
				da_date=trim(rs.fields(0).value)
				da_filenumber=trim(rs.fields(4).value)
				sql_da="insert into dangan(da_date,da_owner,da_filenumber,da_filename,da_recordnumber,da_memo,addid,da_type,da_xls) values('"&da_year&"','"&da_owner&"','"&da_filenumber&"','"&da_filename&"','"&da_recordnumber&"','"&da_memo&"',2,"&da_type&","&da_xls&")"
			end if
			' 业务档案
			if da_type=2 then
				da_year=trim(rs.fields(0).value)
				da_sheet=trim(rs.fields(4).value)

				sql_da="insert into dangan(da_year,da_owner,da_sheet,da_filename,da_recordnumber,da_memo,addid,da_type,da_xls) values("&da_year&",'"&da_owner&"',"&da_sheet&",'"&da_filename&"','"&da_recordnumber&"','"&da_memo&"',2,"&da_type&","&da_xls&")"
			end if
			' response.write sql_da
			' response.end
			call ExecuteCommand(sql_da,false)
			rs.movenext
		loop
		
		rs.close
		set rs=nothing
		xlsconn.close
		set xlsconn=nothing
		response.write("共导入 "&rows&" 条记录<br />")
end sub
%>
		</div>
	</div>
</div>
<!--#include file="lib/footer.asp"-->
</body>
</html>
<!--#include file="lib/connclose.asp"-->

